---
title: Code Generation 알아보기
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
import CodeBlock from "@theme/CodeBlock";
import fetchUser from "!!raw-loader!./about_codegen/main.dart";
import rawFetchUser from "!!raw-loader!./about_codegen/raw.dart";
import {
  trimSnippet,
  CodeSnippet,
} from "../../../../src/components/CodeSnippet";

Code generation is the idea of using a tool to generate code for us.  
In Dart, it comes with the downside of requiring an extra step to "compile"
an application. Although this problem may be solved in the near future, as the
Dart team is working on a potential solution to this problem.

In the context of Riverpod, code generation is about slightly changing the syntax
for defining a "provider". For example, instead of:

<CodeBlock language="dart">{trimSnippet(rawFetchUser)}</CodeBlock>

Using code generation, we would write:

<CodeBlock language="dart">{trimSnippet(fetchUser)}</CodeBlock>

When using Riverpod, code generation is completely optional. It is entirely possible
to use Riverpod without.  
At the same time, Riverpod embraces code generation and recommends using it.

For information on how to install and use Riverpod's code generator, refer to
the [getting started](./getting_started) page. Make sure to enable code generation
in the documentation's sidebar.

## Why use code generation with Riverpod?

You may be wondering: "If code generation is optional in Riverpod, why use it?"

As always with packages: To make your life easier.
This includes but is not limited to:

- better syntax, more readable/flexible and with reduced learning curve.

  - No need to worry about `FutureProvider` vs `Provider` vs etc. Write your logic,
    and Riverpod will pick the most suitable provider for you.
  - Passing parameters to providers is now unrestricted. Instead of being limited to
    using [family](./concepts/modifiers/family) and passing a single positional parameter,
    you can now pass any form of parameter. This includes named parametes, optional ones,
    and even default values.

- **stateful hot-reload** of the code written in Riverpod.
- better debugging, through the generation of extra metadata that the debugger then picks-up.
- some Riverpod features will be available only with code generation.

At the same time, many applications already use code generation with packages such
as [Freezed](https://pub.dev/packages/freezed) or [json_serializable](https://pub.dev/packages/json_serializable).  
In that case, your project probably is already setup for code generation, and
using for Riverpod should be simple.

[hookwidget]: https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/HookWidget-class.html
[statefulwidget]: https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html
[riverpod]: https://github.com/rrousselgit/riverpod
[hooks_riverpod]: https://pub.dev/packages/hooks_riverpod
[flutter_riverpod]: https://pub.dev/packages/flutter_riverpod
[flutter_hooks]: https://github.com/rrousselGit/flutter_hooks
